﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace rkClient
{
    class NativeMethods
    {
        [StructLayout(LayoutKind.Sequential)]
        public struct Message
        {
            public IntPtr hWnd;
            public Int32 msg;
            public IntPtr wParam;
            public IntPtr lParam;
            public uint time;
            public System.Drawing.Point p;
        }

        [System.Security.SuppressUnmanagedCodeSecurity] // We won't use this maliciously
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern bool PeekMessage(out Message msg, IntPtr hWnd, uint messageFilterMin, uint messageFilterMax, uint flags);

    }
    static class Program
    {

        static private GameWindow mainWindow;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            //Open up a main menu with two options
            //1-Create Game(server)
            //2-Join Game(client)
            MainMenu mainMenu = new MainMenu();
            if (mainMenu.ShowDialog() == DialogResult.OK)
            {
                //game should start here
                System.Windows.Forms.Application.Idle += new EventHandler(Application_Idle);


                using (mainWindow = new GameWindow(AppGlobals.PlayerName))
                {
                    //very messy way to set the player color.will try to change it in the future

                    mainWindow.Player.Tank.setColor(AppGlobals.Color);

                    mainWindow.writeToChatTextBox(AppGlobals.PlayerName + " entered the game!");
                    Application.Run(mainWindow);
                }

            }


        }

        static void Application_Idle(object sender, EventArgs e)
        {
            while (mainWindow.AppActive == true && AppStillIdle)
            {
                mainWindow.MainLoop();
            }
        }
        static private bool AppStillIdle
        {
            get
            {
                NativeMethods.Message msg;
                return !NativeMethods.PeekMessage(out msg, IntPtr.Zero, 0, 0, 0);
            }
        }
    }
}
